/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.twitter;

import java.awt.Image;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import nl.cloudfarming.client.messaging.Message;
import nl.cloudfarming.client.messaging.MessageEventKey;
import nl.cloudfarming.eventbus.BaseGuiEventProducer;
import nl.cloudfarming.eventbus.GuiEvent;
import nl.cloudfarming.eventbus.GuiEventKey;
import org.openide.util.Exceptions;
import org.openide.util.ImageUtilities;
import twitter4j.ProfileImage;
import twitter4j.ResponseList;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;

/**
 *
 * @author Timon Veenstra
 */
public class TweetRetriever implements Runnable {

    @Override
    public void run() {
        try {
            Producer producer = new Producer();
            Twitter twitter = new TwitterFactory().getInstance();
            User user = twitter.showUser("AgroSense");
            ResponseList<Status> timeline = twitter.getUserTimeline("AgroSense");
            for (Status status : timeline) {
                producer.sendTweet(status, user);
                System.out.println("@" + user.getScreenName() + " - " + status.getText());
            }
        } catch (TwitterException te) {
            System.out.println("Failed to delete status: " + te.getMessage());
        }
    }

    private class Producer extends BaseGuiEventProducer {

        public Producer() {
            super(MessageEventKey.class);
        }

        public void sendTweet(final Status status, final User user) {
            final Message m = new Message.Builder().setMessageText(status.getText()).setTitle(status.getText().substring(0, 50)).setDateReceived(status.getCreatedAt()).setOrigin(status.getUser().getName()).build();
            m.setTransientMessage(true);
            m.setImage(getImage(user));
            triggerEvent(new GuiEvent() {

                @Override
                public GuiEventKey getKey() {
                    return MessageEventKey.NEW_MESSAGE;
                }

                @Override
                public Object getContent() {
                    return m;
                }
            });
        }
    }

    private Image getImage(User user) {
        ProfileImage.ImageSize imageSize = ProfileImage.MINI;

        try {
            Twitter twitter = new TwitterFactory().getInstance();

            ProfileImage image = twitter.getProfileImage(user.getScreenName(), imageSize);
            Image img =  ImageIO.read(new URL(image.getURL()));
            if (img == null){
                img = ImageUtilities.loadImage("nl/cloudfarming/client/twitter/twitter_newbird_blue.png");
            }else{
                return img;
            }
        } catch (IOException ex) {
            Exceptions.printStackTrace(ex);
        } catch (TwitterException te) {
            Exceptions.printStackTrace(te);
        }
        return null;
    }
}
